package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取webapp真是目录
        String path = request.getServletContext().getRealPath("/static/upload");
        //获取上传文件名称 todo一般上传文件名称改为时间戳 防止重名
        String fileName = file.getOriginalFilename();
        String filePath = path+"/"+fileName;
        if (!file.isEmpty()) {
            file.transferTo(new File(filePath));
            return "uploadSuccess";
        }
        return "uploadFailure";
    }
}